/**
 * Created by litao on 12/10/2016.
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/**
 * Created by Administrator on 2016/12/3.
 */
public class Email {

    private static final String host="smtp.sina.com";
    private static final String host1="pop.sina.com";
    private static final String home_address="litaorui00@sina.com";
    private static final String password="Wsszz123";

    //发送邮件
    public static void sendMail(String target_address) {
        //创建Session实例
        Properties props=System.getProperties();
        props.put("mail.smtp.host",host);
        props.put("mail.smtp.auth","true");
        Session session=Session.getDefaultInstance(props,new Authenticator(){
            @Override
            public PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication(home_address,password);
            }
        });
        try {
            MimeMessage message=new MimeMessage(session);//将Session对象session传递给MimeMessage构造器
            message.setFrom(new InternetAddress(home_address));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(
                    target_address));
            message.setSubject("java");//设置主题
            message.setText("Do you like me?");//设置纯文本消息内容
            Transport.send(message);//消息发送
        }catch(MessagingException e){
            e.printStackTrace();
        }
    }

    //读取邮件
    public static void getReply() throws MessagingException, IOException {
        Properties props = new Properties();
        //创建Session实例
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("pop3");
        store.connect(host1, home_address, password);
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        Message messages[] = folder.getMessages();

        Message message = messages[4];//将Session对象session传递给MimeMessage构造器
        StringBuffer content=new StringBuffer();
        getContent(message,content);
        System.out.println("正文："+content);
        if(content.toString().contains("Yes")) {
            System.out.println(host1+"says that he likes you.");
        }
        else if (content.toString().contains("No")) {
            System.out.println("Wrong!");
        }
        folder.close(false);
        store.close();
    }
    public static void getContent(Part part,StringBuffer content) throws MessagingException, IOException {
        String contentType = part.getContentType();
        if(contentType.contains("text/"))
            content.append(part.getContent().toString());
        else if(contentType.contains("multipart/")){
            Multipart multipart=(Multipart)part.getContent();
            //int partCount=multipart.getCount();
            for(int i=0;i<1;i++)//此处如果判断条件为i<partCount则会同时输出html标签
                getContent(multipart.getBodyPart(i),content);
        }
        if(contentType.contains("Yes"))
            System.out.println(host1+"says that he likes you.");
        else
            System.out.println("Wrong!");

    }


    public static void main(String[] args) throws MessagingException, IOException {

        DB db=new DB("email","rex","123456");
        ArrayList<String> list=new ArrayList();
        list=db.searchAddress();
        System.out.println("Adress: ");
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
            Email.sendMail(list.get(i));//发送邮件
        }

        //Email.sendMail("15071277340m@sina.cn");
        //Email.getReply();//读取邮件内容





    }


}
